package org.dtrd.modules.util;

import org.apache.shiro.SecurityUtils;
import org.dtrd.common.system.vo.LoginUser;

import java.util.Optional;

/**
 * @author qr
 * @date 2021/11/23 9:36 AM
 */
public class ShiroUtil {

    /**
     * 获取正在登陆用户的 orgCode
     */
    public static String getOrgCode(){
        LoginUser user = getLoginUser();
        return Optional.ofNullable(user).map(LoginUser::getOrgCode).orElse(null);
    }

    public static String getLoginUserId(){
        LoginUser user = getLoginUser();
        return Optional.ofNullable(user).map(LoginUser::getId).orElse(null);
    }

    public static String getUserName() {
        LoginUser loginUser = getLoginUser();
        return Optional.ofNullable(loginUser).map(LoginUser::getUsername).orElse(null);
    }

    public static LoginUser getLoginUser(){
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }
}
